//
//  BaiduMapViewController.m
//  MemberProject
//
//  Created by vincent luo on 15/4/30.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "BaiduMapViewController.h"
#import "DetailAddressViewController.h"
@interface BaiduMapViewController ()
@property(assign, nonatomic) CLLocationCoordinate2D userCoordinate;
@end

@implementation BaiduMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.mapView setMapType:BMKMapTypeStandard];
    _mapView.delegate = self;
    [self updateLocation];
    
//    [self addAnnotation];
}
-(void)addAnnotation
{
    
    
    BMKPointAnnotation* annotation = [[BMKPointAnnotation alloc]init];
    CLLocationCoordinate2D coor;
    coor.latitude = _userCoordinate.latitude;
    coor.longitude = _userCoordinate.longitude;
    annotation.coordinate = coor;
    annotation.title = @"当前位置";
    [_mapView addAnnotation:annotation];
    [_mapView setRegion:BMKCoordinateRegionMakeWithDistance(coor, 0.1, 0.1)];
    
}
-(void)updateLocation
{
    [BMKLocationService setLocationDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
    //指定最小距离更新(米)，默认：kCLDistanceFilterNone
    [BMKLocationService setLocationDistanceFilter:100.f];
    
    //初始化BMKLocationService
    _locService = [[BMKLocationService alloc]init];
    _locService.delegate = self;
    //启动LocationService
    [_locService startUserLocationService];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.mapView viewWillAppear];
    _mapView.delegate = self;
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.mapView viewWillDisappear];
    self.mapView.delegate = nil;
    self.locService.delegate = nil;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)didUpdateUserHeading:(BMKUserLocation *)userLocation
{
    //NSLog(@"heading is %@",userLocation.heading);
}
//处理位置坐标更新
- (void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    if (_userCoordinate.longitude==0&&_userCoordinate.latitude==0) {
        _userCoordinate =userLocation.location.coordinate;
        [self addAnnotation];
    }
    
}
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
        BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myAnnotation"];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];
        [btn addTarget:self action:@selector(showDetail:) forControlEvents:64];
        newAnnotationView.rightCalloutAccessoryView =btn;
        [newAnnotationView setSelected:YES animated:YES];
        newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
        return newAnnotationView;
    }
    return nil;
}
-(void)showDetail:(UIButton *)sender
{
    DetailAddressViewController *detailVC = [[DetailAddressViewController alloc]initWithNibName:@"DetailAddressViewController" bundle:nil];
    [detailVC setTargetCoordinate:_userCoordinate];
    [self.navigationController pushViewController:detailVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
